﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace THEMXOASUA
{
    public partial class HOADONWIN : Window
    {
        static HOADON hd = new HOADON();
        static HANGHOA hanghoa = new HANGHOA();
        static HANGHOADAL dal = new HANGHOADAL();
        DataRowView datarow;
        bool addnew = true;
        string shd;
        public HOADONWIN()
        {
            InitializeComponent();
        }
        public HOADONWIN(DataRowView datarow,bool addnew) : this()
        {
            this.datarow=datarow;
            this.addnew = addnew;
            if (addnew == false)
            {
                shd = datarow["sohd"].ToString();
                txtSoHD.Text = shd;
                cboHangHoa.SelectedValue=datarow["mahang"];
                txtDienGiai.Text = datarow["diengiai"].ToString(); 
                txtSoLuong.Text = datarow["soluong"].ToString();
                txtDonGia.Text = datarow["dongia"].ToString();
            }
        }
        public HOADONWIN(DataRowView datarow, int addnew, int shd)
        {

        }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            DataTable dt = hd.DanhSachHoaDon();
            cboHangHoa.ItemsSource = dt.DefaultView;
            cboHangHoa.DisplayMemberPath = "tenhang";
            cboHangHoa.SelectedValuePath = "mahang";

        }
        
        private void Button_Click_1(object sender, RoutedEventArgs e)// nut luu
        {
            string sohd = txtSoHD.Text;
            string mahang = cboHangHoa.SelectedValue.ToString();
             double sl = double.Parse(txtSoLuong.Text);
            double dg = double.Parse(txtDonGia.Text); 
            string diengiai = txtDienGiai.Text;
             double trigia = sl * dg;
             string tenhang = cboHangHoa.Text;
            double khuyenmai = 0.0;
            if (trigia > 20000000)
                khuyenmai = (trigia * 5) / 100;
            else
                khuyenmai = 0.0;
            if (addnew == true)
            {
                hd.LuuHoaDon(sohd, mahang, sl, dg, diengiai);
            }
            else
            {
                hd.SUAHOADON(shd, sohd, mahang,sl,dg, diengiai);
            }

            datarow["sohd"] = sohd;
            datarow["tenhang"] = tenhang;
            datarow["soluong"] = sl;
            datarow["khuyenmai"] = khuyenmai;
            datarow["trigia"]= trigia;
            datarow["dongia"] = dg;
            datarow["diengiai"] = diengiai;
            txtSoHD.Text = "";
            txtDonGia.Text = " ";
            txtSoLuong.Text = " ";
            txtDienGiai.Text = " ";
            txtSoHD.Focus();
            this.Close();

            

        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
